import Vue from 'vue';
import ElementUI from 'element-ui';
import moment from 'moment';
import numeral from 'numeral';
import 'babel-polyfill';
import VueClipboard from 'vue-clipboard2';
import environments from './utils/environments';
import i18n from './utils/lang';
import '../static/theme/theme-278EDA/index.css';
import store from './store';
import instance from './utils/axios';
import router from './router';
import './router/action';
import './assets/css/theme.scss';
import './assets/css/normalize.scss';
import App from './App';

// 复制文本插件
Vue.use(VueClipboard)

// 国际化 - 语言
Vue.use(ElementUI, {
  i18n: (key, value) => i18n.t(key, value)
});

Vue.config.productionTip = false;

Vue.prototype.$http = instance;
Vue.prototype.$appName = environments;

// 输入框只允许输入正整数
const trigger = (el, type) => {
  const e = document.createEvent('HTMLEvents')
  e.initEvent(type, true, true)
  el.dispatchEvent(e)
}
Vue.directive('Int', {
  bind(el) {
    const input = el.getElementsByTagName('input')[0]
    input.onkeyup = (() => {
      if (input.value.length === 1) {
        input.value = input.value.replace(/[^1-9]/g, '')
      } else {
        input.value = input.value.replace(/[^\d]/g, '')
      }
      trigger(input, 'input')
    })
    input.onblur = (() => {
      if (input.value.length === 1) {
        input.value = input.value.replace(/[^1-9]/g, '')
      } else {
        input.value = input.value.replace(/[^\d]/g, '')
      }
      trigger(input, 'input')
    })
  }
});


// unix 时间戳、 utc Data类型
Vue.filter('dateFormat', value => moment.utc(value).format('YYYY-MM-DD'));
Vue.filter('dateFormatYMDF', value => moment(value).format('YYYY-MM-DD HH:mm'));
Vue.filter('dateFormatAll', value => moment.utc(value).format('YYYY-MM-DD HH:mm:ss'));
Vue.filter('dateFormatMonth', value => moment.utc(value).format('MM-DD'));

Vue.filter('dateFormatBIH', value => moment(value).format('YYYY-MM-DD HH:mm:ss'));
Vue.filter('dateFormatBIHM', value => moment(value).format('YYYY-MM-DD HH:mm'));
Vue.filter('dateFormatTimeBIH', value => moment(value).format('HH:mm:ss'));
Vue.filter('dateFormatMonthHour', value => moment(value).format('MM-DD HH:mm:ss'));
Vue.filter('dateFormatT', (value) => {
  const zone = moment().zone() / 60; // 时区
  return moment(value).add(0 - zone, 'hours').format('MM-DD HH:mm:ss');
});
Vue.filter('dateFormatTYear', (value) => {
  const zone = moment().zone() / 60; // 时区
  return moment(value).add(0 - zone, 'hours').format('YYYY-MM-DD HH:mm:ss');
});
// 位数分割
Vue.filter('numeralFormat', (value, p) => {
  let xstr = '0';
  if (p) {
    xstr = '0.';
    for (var i = 0; i < p; i++) { // eslint-disable-line
      xstr += '0';  // eslint-disable-line
    }
  }
  return numeral(value).format(`0,${xstr}`);
});

/* eslint-disable no-new */
new Vue({
  router,
  store,
  i18n,
  components: { App },
  template: '<App/>',
}).$mount('#app');
